#include "mq_channal.hpp"
#include "mq_connection.hpp"
#include "mq_worker.hpp"
#include <chrono>
#include <functional>
#include <memory>
#include <string>
#include <thread>
void cb(const mq_zxws::Channal::ptr & channal, const std::string tag, const mq_zxws::BasicProperties* bp, const std::string body)
{
    ILOG("%s 收到消息 %s", tag.c_str(), body.c_str());
    channal->basicAck(bp->id());
}
int main(int argc, char* argv[])
{
    if(argc != 2){
        std::cout << "Usage:\n";
        std::cout << "\t" << argv[0] << " " << "queue\n";
        return -1;
    }
    // 1、实例化异步工作线程池
    mq_zxws::AsyncWorker::ptr awp = std::make_shared<mq_zxws::AsyncWorker>();
    DLOG("1");
    // 2、实例化connection对象
    mq_zxws::Connection::ptr conn = std::make_shared<mq_zxws::Connection>("127.0.0.1", 8080, awp);
    DLOG("2");
    // 3、创建信道
    mq_zxws::Channal::ptr channal = conn->openChannal();
    DLOG("3");
    // 4、通过信道提供的服务完成用户需求
    //   1）声明一个交换机exchange1，交换机类型为广播模式
    google::protobuf::Map<std::string, std::string> tmp;
    channal->declareExchange("exchange1", mq_zxws::ExchangeType::TOPIC, true, false, tmp);
    DLOG("4");
    //   2）声明一个队列queue1
    channal->declareQueue("queue1", true, false, false, tmp);
    DLOG("5");
    //   3）声明一个队列queue2
    channal->declareQueue("queue2", true, false, false, tmp);
    DLOG("6");
    //   4）绑定queue1—exchange1，且binding_key设置为queue1
    channal->queueBind("exchange1", "queue1", "queue1");
    DLOG("7");
    //   5）绑定queue2—exchange1，且binding_key设置为news.music.#
    channal->queueBind("exchange1", "queue2", "news.music.#");
    DLOG("8");
    // 5、订阅消息ConsumerCallBack 
    auto func = std::bind(cb, channal, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
    DLOG("9");
    channal->basicConsumer("consumer1", argv[1], false, func);
    DLOG("10");
    while(1) std::this_thread::sleep_for(std::chrono::seconds(3));
    // 6、关闭信道
    channal->closeChannal();
    return 0;
}